package com.cho.core;

import java.io.File;
import java.io.FilenameFilter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.cho.core.util.StringUtils;

public class CustomProgramFileExtensionFilter implements FilenameFilter {
	private Logger logger =  Logger.getLogger(this.getClass());
	private static Set<String> allProgramLanguageFileExtensionSet = new HashSet<String>();
	private static Set<String> SqlFileExtensionSet = new HashSet<String>();

	{
		// web
		allProgramLanguageFileExtensionSet.add("jsp");
		allProgramLanguageFileExtensionSet.add("asp");
		allProgramLanguageFileExtensionSet.add("php");

		// language
		allProgramLanguageFileExtensionSet.add("java");

		// c
		allProgramLanguageFileExtensionSet.add("c");
		allProgramLanguageFileExtensionSet.add("cc");
		allProgramLanguageFileExtensionSet.add("cpp");
		allProgramLanguageFileExtensionSet.add("cs");
		allProgramLanguageFileExtensionSet.add("m");
		allProgramLanguageFileExtensionSet.add("mm");
		allProgramLanguageFileExtensionSet.add("vb");

		// 파이썬
		allProgramLanguageFileExtensionSet.add("py");
		
		//그외 ""에 감싸지 않은 sql
		SqlFileExtensionSet.add("xml");
		SqlFileExtensionSet.add("pc");
		SqlFileExtensionSet.add("xc");
		SqlFileExtensionSet.add("kc");
		SqlFileExtensionSet.add("kx");
		
		allProgramLanguageFileExtensionSet.addAll(SqlFileExtensionSet);
	}

	public CustomProgramFileExtensionFilter() {
		// TODO Auto-generated constructor stub
	}

	public CustomProgramFileExtensionFilter(String fileExtensionList) {
		super();

		if (StringUtils.isNotEmpty(fileExtensionList)) {

			String[] fileExtentsions = fileExtensionList.split(",");
			for (String fileExtentsion : fileExtentsions) {
				this.allProgramLanguageFileExtensionSet.add(fileExtentsion
						.trim().toLowerCase());
			}
		}
	}

	@Override
	public boolean accept(File dir, String name) {
		if(dir.isDirectory()){
			//폴더이면 return 한다.
			return true;
		}
		logger.debug("파일명: " + name);
		if (dir != null && name != null) {
			return includeFileNameEndWithFilter(name);
		}
		return false;
	}

	private boolean includeFileNameEndWithFilter(String name) {
		return fileNameEndWithFilter(name,
				this.allProgramLanguageFileExtensionSet, true, false);
	}

	private boolean excludeFileNameEndWithFilter(String name) {
		return fileNameEndWithFilter(name,
				this.allProgramLanguageFileExtensionSet, false, true);
	}

	private boolean fileNameFilter(String name, final List<String> fileName,
			boolean findSuccess, boolean findFail) {
		for (String fileter : fileName) {
			if (name.indexOf(fileter) > -1) {
				return findSuccess;
			}
		}
		return findFail;
	}

	private boolean fileNameStartWithFilter(String name,
			final List<String> fileNameStartsWith, boolean findSuccess,
			boolean findFail) {
		for (String prefix : fileNameStartsWith) {
			if (name.startsWith(prefix)) {
				return findSuccess;
			}
		}
		return findFail;
	}

	private boolean fileNameEndWithFilter(String name,
			final Set<String> fileNameEndsWith, boolean findSuccess,
			boolean findFail) {
		for (String suffix : fileNameEndsWith) {
			if (name.toLowerCase().endsWith("." + suffix.toLowerCase())) {
				return findSuccess;
			}
		}
		return findFail;
	}

	
	
	public static Set<String> getAllProgramLanguageFileExtensionSet() {
		return allProgramLanguageFileExtensionSet;
	}


	public static Set<String> getSqlFileExtensionSet() {
		return SqlFileExtensionSet;
	}

	public static void main(String[] args) {

		CustomProgramFileExtensionFilter t = new CustomProgramFileExtensionFilter();
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.jsp"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.JSP"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.asp"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.svn"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.cst"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.aa"));
		System.out.println();
		t = new CustomProgramFileExtensionFilter("cst");
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.jsp"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.JSP"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.asp"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.svn"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.cst"));
		System.out.println(t.includeFileNameEndWithFilter("sfsdf.aa"));
	}
	
	
	

}
